var app = getApp()
//常量数据配置
var ConstantData = require('../../../../../../../common/constant_data.js')
var uitl = require('../../../../../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    

    var that = this;
    var lbmId = options.lbmId;
    var hospitalName = options.hospitalName;
    that.setData({
      hospitalName: hospitalName,
      lbmId: lbmId
    });

    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/maintain/check_piple',
        data: {
          lbmId: lbmId
        },
        header: header,
        success: function (res) {
          getApp().hideLoading();
          if (res.data.result === 0) {
            var pipleList = res.data.data;
            var pipleMap = new Array();
            var postionArr = new Array();
            var positionPipleNumArr = new Array();
            var overTimeLen = 3 * 24 * 60 * 60 * 1000;

            for(var i=0;i<pipleList.length;i++) {
              if (!pipleList[i].created || !pipleList[i].hpe_electricity) {
                pipleList[i].isMark = true;
              }
              var timestamp = new Date().getTime();
              var diff = timestamp - pipleList[i].created;
              if (diff >= overTimeLen || pipleList[i].hpe_electricity < 10000 || pipleList[i].hp_stat != 1 || pipleList[i].oneMinuteInfo) {
                pipleList[i].isMark = true;
                pipleList[i].diff = diff;
                pipleList[i].overTimeLen = overTimeLen;
              } else {
                pipleList[i].isMark = false;
              }
              pipleList[i].created = uitl.timestampToTime(pipleList[i].created);

              var position = pipleList[i].hp_position;
              var index = postionArr.indexOf(position);
              if(index == -1) {
                  postionArr.push(position);
                  index = postionArr.indexOf(position);
              }

              if(!pipleMap[index]) {
                pipleMap[index] = new Array();
              }
              pipleMap[index].push(pipleList[i]);

              if (!positionPipleNumArr[index]) {
                positionPipleNumArr[index] = new Array();
              }
              positionPipleNumArr[index].push(pipleList[i].id);

            }
            console.log("pipleList-->", pipleList);
            console.log("pipleMap-->",pipleMap);
            that.setData({
              pipleList: pipleList,
              pipleInfoList: pipleMap,
              pipleInfoOriginList:pipleMap,
              homingStatInfo: res.data.pipleStatInfo,
              postionArr: postionArr,
              postionOriginArr: postionArr,
              positionPipleNumArr: positionPipleNumArr
            })
          }
          that.setData({
            noDataDesc: "尚未发现可能下线的桩"
          })
        }
      })
    });  
     

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  /**
   * 输入关键字事件
   */
  inputKey:function(e) {
    var inputKey = e.detail.value;
    
    var pipleInfoList = new Array();
    var pipleList = this.data.pipleList;
    var positionArr = new Array();
    for (var i = 0; i < pipleList.length;i++) {
      if (pipleList[i].id == inputKey) {
        positionArr.push(pipleList[i].hp_position);
        var temp = new Array();
        temp.push(pipleList[i]);
        pipleInfoList.push(temp);
        break;
      }
    }

    if (!inputKey) {
      pipleInfoList = this.data.pipleInfoOriginList;
      positionArr = this.data.postionOriginArr;
    }

    this.setData({
      pipleInfoList: pipleInfoList,
      postionArr: positionArr
    })

  }

})